<template>
  <div>
    <el-radio-group
      v-model="radioList.radioData"
      @change="selectChange"
      :disabled="disabled"
    >
      <el-radio
        v-for="item in radioList.options"
        :key="item.code"
        :label="item.code"
        >{{ item.name }}</el-radio
      >
    </el-radio-group>
  </div>
</template>
<script setup name="ModelRadio">
import { getDictItemByDictCode } from '@/api/base.js'
const { proxy } = getCurrentInstance()
const props = defineProps(['modelValue', 'typeCode', 'disabled'])
const emit = defineEmits(['returnBack', 'change', 'update:modelValue'])

const radioList = reactive({
  options: [],
  radioData: '',
  defaultValue: '',
})
const state = reactive({
  selectValue: '',
})
watch(
  () => props.modelValue,
  (val) => {
    if (val || val === 0) {
      radioList.radioData = val
    } else {
      state.selectValue = radioList.defaultValue
      emit('update:modelValue', radioList.defaultValue)
      emit('returnBack', radioList.defaultValue)
    }
  },
  {
    immediate: true,
    deep: true,
  }
)

// model: {
//   prop: "modelValue",
//   event: "returnBack",
// },

onMounted(() => {
  getOptions()
})

function selectChange(data) {
  emit('update:modelValue', data)
  emit('returnBack', data)
  emit('change', data)
}
function getOptions() {
  let result =
    proxy.$fn.deepClone(proxy.$store.state.permission.dict[props.typeCode]) ||
    []
  result.forEach((item) => {
    try {
      item.code = Number(item.code)
    } catch (err) {
      item.code = item.code
    }
  })
  radioList.options = result
  if (props.modelValue || props.modelValue === 0) {
    radioList.radioData = props.modelValue
  } else {
    for (let item of result) {
      if (item.isDefault === 1) {
        radioList.defaultValue = item.code
        radioList.radioData = item.code
        emit('update:modelValue', item.code)
        emit('returnBack', item.code)
        break
      }
    }
  }
  /* getDictItemByDictCode(props.typeCode)
    .then((resp) => {
      if (resp && resp.success) {
        result.forEach((item) => {
          try {
            item.code = Number(item.code)
          } catch (err) {
            item.code = item.code
          }
        })
        radioList.options = resp.data
        if (props.modelValue || props.modelValue === 0) {
          radioList.radioData = props.modelValue
        } else {
          for (let item of resp.data) {
            if (item.isDefault === 1) {
              radioList.defaultValue = item.code
              radioList.radioData = item.code
              emit('update:modelValue', item.code)
              emit('returnBack', item.code)
              break
            }
          }
        }
      }
    })
    .catch((err) => {}) */
}
</script>

<style scoped></style>
